

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import state.State;
import tool.KeyCode;
import boot.Boot;
import db.ArtistRecord;
import db.Mp3DBRecord;
import db.SimpleDBReader;
import files.Files;
import function.search.EventListener;
import function.search.SearchKeyHandler2;
import graphics.Background;
import graphics.BootColor;
import graphics.SearchChoiceInput2;
import graphics.SearchInput2;

public class ArtistSearchScreen extends Canvas implements CommandListener,
		EventListener {

	private SearchInput2 searchInput;
	private SearchKeyHandler2 searchKeyHandler;
	private SimpleDBReader dbReader;
	private SearchChoiceInput2 artistChoiceInput;
	private SearchChoiceInput2 tuneChoiceInput;
	private String searchInputValue;
	private ArtistRecord[] artistsRecords;
	private Mp3DBRecord[] chosenMp3Records;
	private ArtistChoiceInputListener artistChoiceListener;
	private Background background;
	private Boot boot;
	private final Command cancelSearchCommand = new Command("Cancel",
			Command.EXIT, 1);

	public ArtistSearchScreen() {
		boot = Boot.getInstance();
		addCommand(cancelSearchCommand);
		setCommandListener(this);
		reboot();
	}

	protected void paint(Graphics g) {

		background.repaint(g);

		if (Boot.state().equals(State.ARTIST_SEARCH_ENGAGED)) {
			searchInput.repaint(g);
		} else if (Boot.state().equals(State.DISPLAY_ARTISTS_FROM_SEARCH)) {
			artistChoiceInput.repaint(g);
		} else if (Boot.state().equals(State.DISPLAY_TUNES_FROM_ARTIST)) {
			tuneChoiceInput.repaint(g);
		}

	}

	public void commandAction(Command command, Displayable dis) {
		if (command == cancelSearchCommand) {
			reboot();
			repaint();
		}
	}

	public void keyPressed(int keyCode) {

		if (Boot.state().equals(State.DISPLAY_ARTISTS_FROM_SEARCH)) {
			artistChoiceInput.keyPressed(keyCode);
			repaint();
		} else if (Boot.state().equals(State.DISPLAY_TUNES_FROM_ARTIST)) {
			tuneChoiceInput.keyPressed(keyCode);
			repaint();
		} else if (Boot.state().equals(State.ARTIST_SEARCH_ENGAGED)) {
			searchKeyHandler.keyPressed(keyCode);
			repaint();

			if ((keyCode == KeyCode.HAUT) || (keyCode == KeyCode.BAS)) {
				Boot.state().remove(State.ARTIST_SEARCH_ENGAGED);
				Boot.state().remove(State.DISPLAY_ARTISTS_FROM_SEARCH);
				Boot.state().remove(State.DISPLAY_TUNES_FROM_ARTIST);
				Boot.state().remove(State.MODE_SEARCH_BY_ARTIST);
				Boot.state().add(State.MODE_SEARCH_BY_TITLE);
				boot.displayRelevantScreen();
			}
		}

	}

	public void reboot() {
		background = new Background();
		int numKey = 3;
		searchInput = new SearchInput2(this, numKey, BootColor.SILVER);
		searchKeyHandler = new SearchKeyHandler2(numKey, searchInput);
		searchKeyHandler.addListener(this);
		Boot.state().remove(State.DISPLAY_ARTISTS_FROM_SEARCH);
		Boot.state().remove(State.DISPLAY_TUNES_FROM_ARTIST);
		Boot.state().add(State.ARTIST_SEARCH_ENGAGED);
	}

	private void setupSearchChoiceInput() {
		String[] artists = getArtists();
		artistChoiceInput = new SearchChoiceInput2(artists);
		artistChoiceListener = new ArtistChoiceInputListener();
		artistChoiceInput.addListener(artistChoiceListener);
	}

	private String[] getArtists() {
		String[] result = new String[artistsRecords.length];
		for (int i = 0; i < artistsRecords.length; i++) {
			result[i] = artistsRecords[i].getArtistName();
		}
		return result;
	}

	public void processEvent(String event) {
		if (EventListener.EVENT_PROCESS_DONE.equals(event)) {
			searchInputValue = searchKeyHandler.getSearchInput();
			computeArtistsRecords();
			setupSearchChoiceInput();

			Boot.state().remove(State.ARTIST_SEARCH_ENGAGED);
			Boot.state().add(State.DISPLAY_ARTISTS_FROM_SEARCH);
			repaint();
		}
	}

	private void computeArtistsRecords() {

		dbReader = new SimpleDBReader(Files.BOOT_ROOT + "/Artists.db");
		ArtistsDBListener listener = new ArtistsDBListener(searchInputValue,
				dbReader);
		dbReader.addListener(listener);

		dbReader.readDB();
		artistsRecords = listener.getArtistRecords();
	}

	private class ArtistChoiceInputListener implements EventListener {

		private ArtistRecord chosenArtist;

		public void processEvent(String event) {
			if (EventListener.EVENT_PROCESS_DONE.equals(event)) {
				int chosenIndex = artistChoiceInput.getchosenIndex();
				chosenArtist = artistsRecords[chosenIndex];
				chosenMp3Records = chosenArtist.getRecords();
				String[] titles = new String[chosenMp3Records.length];
				for (int i = 0; i < titles.length; i++) {
					titles[i] = chosenMp3Records[i].getTitle();
				}
								
				tuneChoiceInput = new SearchChoiceInput2(titles);
				tuneChoiceInput.engageAllMode();
				tuneChoiceInput.addListener(new EventListener() {

					public void processEvent(String event) {
						if (EventListener.EVENT_PROCESS_DONE.equals(event)) {
							int selection = tuneChoiceInput.getchosenIndex();
							Mp3DBRecord record = chosenMp3Records[selection];
							reboot();
							boot.displayRelevantScreen();
							boot.searchChoiceCompleted(record.getMp3File());
						} else if (EventListener.EVENT_ALL_SELECTED.equals(event)) {
							Mp3DBRecord record = chosenMp3Records[0];
							boot.displayRelevantScreen();
							boot.searchChoiceCompleted(record.getMp3File());
							for (int i = 1; i < chosenMp3Records.length; i++) {
								boot.enqueueMp3(chosenMp3Records[i].getMp3File());
							}
							boot.alert("All tunes added", 1000);
							reboot();
						}
						
					}});
				Boot.state().remove(State.DISPLAY_ARTISTS_FROM_SEARCH);
				Boot.state().add(State.DISPLAY_TUNES_FROM_ARTIST);
			}
		}

	}
}
